//
//  CXHToBidRewardVideoAd.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/9.
//

#import "CXHToBidRewardVideoAd.h"
#import <CXHAdSDK-Core/CXHAdSDKRewardVideoAd.h>
#import <WindMillSDK/WindMillSDK.h>
#import <WindFoundation/WindFoundation.h>

@interface CXHToBidRewardVideoAd ()<CXHAdSDKRewardVideoAdDelegate>

@property (nonatomic, weak) id<AWMCustomRewardedVideoAdapterBridge> bridge;
@property (nonatomic, weak) id<AWMCustomRewardedVideoAdapter> adapter;
@property (nonatomic, strong) CXHAdSDKRewardVideoAd *rewardedVideoAd;
@property (nonatomic, assign) BOOL isReady;

@end

@implementation CXHToBidRewardVideoAd

- (instancetype)initWithBridge:(id<AWMCustomAdapterBridge>)bridge adapter:(id<AWMCustomAdapter>)adapter {
    self = [super init];
    if (self) {
        _bridge = (id<AWMCustomRewardedVideoAdapterBridge>)bridge;
        _adapter = (id<AWMCustomRewardedVideoAdapter>)adapter;
    }
    return self;
}
- (BOOL)mediatedAdStatus {
    return self.isReady;
}
- (void)loadAdWithPlacementId:(NSString *)placementId parameter:(AWMParameter *)parameter {
    self.rewardedVideoAd = [[CXHAdSDKRewardVideoAd alloc]init];
    self.rewardedVideoAd.posId = placementId;
    self.rewardedVideoAd.delegate = self;
    [self.rewardedVideoAd loadAdData];
    
}

- (BOOL)showAdFromRootViewController:(UIViewController *)viewController parameter:(AWMParameter *)parameter {
    [self.rewardedVideoAd showRewardVideoAdFromRootViewController:viewController];
    return YES;
}
- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.rewardedVideoAd sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.rewardedVideoAd sendLossNotice:result.winnerPrice];
}
- (void)dealloc {
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
}

#pragma mark - CXHAdSDKRewardVideoAdDelegate
/**
 广告数据加载成功回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidLoad:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    NSString *price = [NSString stringWithFormat:@"%ld", [self.rewardedVideoAd getBidPrice]];
    [self.bridge rewardedVideoAd:self.adapter didAdServerResponseWithExt:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    self.isReady = YES;
    [self.bridge rewardedVideoAdDidLoad:self.adapter];
}

///**
// 视频数据下载成功回调，已经下载过的视频会直接回调
//
// @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
// */
//- (void)CXH_rewardVideoAdVideoDidLoad:(CXHAdSDKRewardVideoAd *)rewardedVideoAd;

/**
 视频播放页即将展示回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdWillVisible:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频广告曝光回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidExposed:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge rewardedVideoAdDidVisible:self.adapter];
}

/**
 视频广告曝光上报回调（只会回调一次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidExposeReport:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频播放页关闭回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClose:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge rewardedVideoAdDidClose:self.adapter];
}

/**
 视频广告信息点击回调（可能回调多次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClicked:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge rewardedVideoAdDidClick:self.adapter];
}

/**
 视频广告信息点击上报回调（只会回调一次）

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidClickReport:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    
}

/**
 视频广告各种错误信息回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 @param error 具体错误信息
 */
- (void)CXH_rewardVideoAd:(CXHAdSDKRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    self.isReady = NO;
    [self.bridge rewardedVideoAd:self.adapter didLoadFailWithError:error ext:nil];
}


/**
 视频广告播放达到激励条件回调

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidRewardEffective:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    WindMillRewardInfo *info = [[WindMillRewardInfo alloc] init];
    info.isCompeltedView = YES;
    [self.bridge rewardedVideoAd:self.adapter didRewardSuccessWithInfo:info];
}

/**
 视频广告视频播放完成

 @param rewardedVideoAd CXHAdSDKRewardVideoAd 实例
 */
- (void)CXH_rewardVideoAdDidPlayFinish:(CXHAdSDKRewardVideoAd *)rewardedVideoAd{
    WindmillLogDebug(@"[cxh]", @"%@", NSStringFromSelector(_cmd));
    [self.bridge rewardedVideoAd:self.adapter didPlayFinishWithError:nil];
}

@end
